<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/2
 * Time: 17:24
 */

namespace App\Admin\Models;

/**
 * App\Admin\Models\EmployeeDetailModel
 *
 * @property int                                  $detail_id            详情ID
 * @property int                                  $employee_id          员工ID，admin_employees表主键
 * @property string|null                          $education_background 教育经历
 * @property string|null                          $work_experience      工作经历
 * @property string|null                          $membership           成员资格
 * @property string|null                          $academic_experience  学术任职经历
 * @property string|null                          $research_project     科研项目
 * @property string|null                          $award_experience     获奖经历
 * @property string|null                          $publication          发表著作
 * @property string|null                          $custom_text_zh       自定义扩展html-中文
 * @property string|null                          $custom_text_en       自定义扩展html-英文
 * @property \Illuminate\Support\Carbon           $created_at           创建时间
 * @property \Illuminate\Support\Carbon           $updated_at           最后更新时间
 * @property-read \App\Admin\Models\EmployeeModel $employee
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereAcademicExperience($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereAwardExperience($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereCustomTextEn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereCustomTextZh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereDetailId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereEducationBackground($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereEmployeeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereMembership($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel wherePublication($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereResearchProject($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeDetailModel whereWorkExperience($value)
 * @mixin \Eloquent
 */
class EmployeeDetailModel extends BaseModel
{
    protected $table = 'admin_employees_detail';

    protected $primaryKey = 'detail_id';

    protected $fillable = [
        'employee_id',
        'education_background',
        'work_experience',
        'membership',
        'academic_experience',
        'research_project',
        'award_experience',
        'publication',
        'custom_text_zh',
        'custom_text_en',
    ];

    protected $guarded = [];

    public function getEducationBackgroundAttribute($value)
    {
        if (!empty($value) && is_string($value)) {
            $valueData = json_decode($value, true);
            if (!empty($valueData) && empty($valueData[0]['index'])) {
                foreach ($valueData as $key => &$item) {
                    $item['index'] = $key + 1;
                }
                unset($item);
            }
            $value = json_encode($valueData);
        }
        return array_values(json_decode($value, true) ?: []);
    }

    public function setEducationBackgroundAttribute($value)
    {
        if (is_array($value) && !empty($value[0]['index'])) {
            // 二维数组从小到大排序
            $indexs = array_column($value, 'index');
            array_multisort($indexs, SORT_ASC, $value);
        }
        $this->attributes['education_background'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getWorkExperienceAttribute($value)
    {
        if (!empty($value) && is_string($value)) {
            $valueData = json_decode($value, true);
            if (!empty($valueData) && empty($valueData[0]['index'])) {
                foreach ($valueData as $key => &$item) {
                    $item['index'] = $key + 1;
                }
                unset($item);
            }
            $value = json_encode($valueData);
        }
        return array_values(json_decode($value, true) ?: []);
    }

    public function setWorkExperienceAttribute($value)
    {
        if (is_array($value) && !empty($value[0]['index'])) {
            // 二维数组从小到大排序
            $indexs = array_column($value, 'index');
            array_multisort($indexs, SORT_ASC, $value);
        }
        $this->attributes['work_experience'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getMembershipAttribute($value)
    {
        if (!empty($value) && is_string($value)) {
            $valueData = json_decode($value, true);
            if (!empty($valueData) && empty($valueData[0]['index'])) {
                foreach ($valueData as $key => &$item) {
                    $item['index'] = $key + 1;
                }
                unset($item);
            }
            $value = json_encode($valueData);
        }
        return array_values(json_decode($value, true) ?: []);
    }

    public function setMembershipAttribute($value)
    {
        if (is_array($value) && !empty($value[0]['index'])) {
            // 二维数组从小到大排序
            $indexs = array_column($value, 'index');
            array_multisort($indexs, SORT_ASC, $value);
        }
        $this->attributes['membership'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getAcademicExperienceAttribute($value)
    {
        if (!empty($value) && is_string($value)) {
            $valueData = json_decode($value, true);
            if (!empty($valueData) && empty($valueData[0]['index'])) {
                foreach ($valueData as $key => &$item) {
                    $item['index'] = $key + 1;
                }
                unset($item);
            }
            $value = json_encode($valueData);
        }
        return array_values(json_decode($value, true) ?: []);
    }

    public function setAcademicExperienceAttribute($value)
    {
        if (is_array($value) && !empty($value[0]['index'])) {
            // 二维数组从小到大排序
            $indexs = array_column($value, 'index');
            array_multisort($indexs, SORT_ASC, $value);
        }
        $this->attributes['academic_experience'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getResearchProjectAttribute($value)
    {
        if (!empty($value) && is_string($value)) {
            $valueData = json_decode($value, true);
            if (!empty($valueData) && empty($valueData[0]['index'])) {
                foreach ($valueData as $key => &$item) {
                    $item['index'] = $key + 1;
                }
                unset($item);
            }
            $value = json_encode($valueData);
        }
        return array_values(json_decode($value, true) ?: []);
    }

    public function setResearchProjectAttribute($value)
    {
        if (is_array($value) && !empty($value[0]['index'])) {
            // 二维数组从小到大排序
            $indexs = array_column($value, 'index');
            array_multisort($indexs, SORT_ASC, $value);
        }
        $this->attributes['research_project'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getAwardExperienceAttribute($value)
    {
        if (!empty($value) && is_string($value)) {
            $valueData = json_decode($value, true);
            if (!empty($valueData) && empty($valueData[0]['index'])) {
                foreach ($valueData as $key => &$item) {
                    $item['index'] = $key + 1;
                }
                unset($item);
            }
            $value = json_encode($valueData);
        }
        return array_values(json_decode($value, true) ?: []);
    }

    public function setAwardExperienceAttribute($value)
    {
        if (is_array($value) && !empty($value[0]['index'])) {
            // 二维数组从小到大排序
            $indexs = array_column($value, 'index');
            array_multisort($indexs, SORT_ASC, $value);
        }
        $this->attributes['award_experience'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getPublicationAttribute($value)
    {
        if (!empty($value) && is_string($value)) {
            $valueData = json_decode($value, true);
            if (!empty($valueData) && empty($valueData[0]['index'])) {
                foreach ($valueData as $key => &$item) {
                    $item['index'] = $key + 1;
                }
                unset($item);
            }
            $value = json_encode($valueData);
        }
        return array_values(json_decode($value, true) ?: []);
    }

    public function setPublicationAttribute($value)
    {
        if (is_array($value) && !empty($value[0]['index'])) {
            // 二维数组从小到大排序
            $indexs = array_column($value, 'index');
            array_multisort($indexs, SORT_ASC, $value);
        }
        $this->attributes['publication'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    /**
     * 和EmployeeModel一对一的关系
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function employee()
    {
        return $this->belongsTo(EmployeeModel::class, 'employee_id');
    }
}
